/**
 * Created by PanJiaChen on 16/11/18.
 */

/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str) {
  const valid_map = ['admin', 'editor']
  return valid_map.indexOf(str.trim()) >= 0
}

/**
 * 获取本月第一天0点0分0秒j及本天23点59分59秒
 * @returns yy-mm-dd
 */
export function getMonthStartTime() {
  // 时间戳转换
  function formatDateTime(inputTime) {
    const date = new Date(inputTime)
    const y = date.getFullYear()
    let m = date.getMonth() + 1
    m = m < 10 ? ('0' + m) : m
    let d = date.getDate()
    d = d < 10 ? ('0' + d) : d
    let h = date.getHours()
    h = h < 10 ? ('0' + h) : h
    let minute = date.getMinutes()
    let second = date.getSeconds()
    minute = minute < 10 ? ('0' + minute) : minute
    second = second < 10 ? ('0' + second) : second
    return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second
  }

  // 获取本天23点59分59秒
  let end = new Date().toLocaleDateString()
  end = new Date(end).getTime() + 24 * 60 * 60 * 1000 - 1
  end = new Date(end)

  // 获取本月第一天0点0分0秒
  const start = new Date()
  start.setDate(1)
  start.setHours(0)
  start.setSeconds(0)
  start.setMinutes(0)

  const objData = {
    start: formatDateTime(start.getTime()),
    end: formatDateTime(end)
  }
  return objData
}
